#region Copyright
// Copyright (c) 2010, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>.
// This source is subject to the Microsoft Public License. 
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 
// All other rights reserved.
#endregion

namespace LiveWriter.Plugins.SyntaxHighlighter
{
    using System;
    using System.Windows.Forms;

    internal partial class CodeEditor : CodeEditorBase
    {
        public CodeEditor()
        {
            InitializeComponent();
            updFirstLine.Maximum = int.MaxValue;
            cboLanguages.Load(SupportedSettings.Languages);
        }

        protected override void LoadProperties()
        {
            cboLanguages.SetSelectedIndex(Properties.Language());
            txtCode.Text = Properties.Code();
            chkAutoLink.Checked = Properties.AutoLinks();
            chkCollapse.Checked = Properties.Collapse();
            chkSmartTabs.Checked = Properties.SmartTabs();
            chkGutter.Checked = Properties.Gutter();
            chkToolbar.Checked = Properties.Toolbar();
            chkHtmlScript.Checked = Properties.HtmlScript();
            updTabSize.Value = Properties.TabSize();
            updFirstLine.Value = Properties.FirstLine();
            txtHighlightedLines.Text = Properties.HighlightLines();
            txtCaption.Text = Properties.Caption();
        }

        protected override void SaveProperties()
        {
            Properties.Language(((Pair) cboLanguages.SelectedItem).Key);
            Properties.Code(txtCode.Text.Trim());
            Properties.AutoLinks(chkAutoLink.Checked);
            Properties.Collapse(chkCollapse.Checked);
            Properties.SmartTabs(chkSmartTabs.Checked);
            Properties.Gutter(chkGutter.Checked);
            Properties.Toolbar(chkToolbar.Checked);
            Properties.HtmlScript(chkHtmlScript.Checked);
            Properties.TabSize(Convert.ToInt32(updTabSize.Value));
            Properties.FirstLine(Convert.ToInt32(updFirstLine.Value));
            Properties.HighlightLines(txtHighlightedLines.Text.Trim());
            Properties.Caption(txtCaption.Text.Trim());
        }

        private void FixIndentation()
        {
            int tabSize = Convert.ToInt32(updTabSize.Value);

            txtCode.Text = txtCode.Text.FixIndentation(tabSize);
            txtCode.SelectionStart = 0;
            txtCode.SelectionLength = 0;
        }

        private void btnIndentation_Click(object sender, EventArgs e)
        {
            FixIndentation();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            SaveProperties();
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}